package com.YxyBackMange.StatcManger;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/19.
 */
public class StaticManger {
    private static boolean isStatic;//是否开启静态化
    private static List<StaticConfig> configs = new ArrayList<StaticConfig>();
    private static boolean isDebug = true;//是否打印静态化日志
    private static String path;
    public StaticManger() {
    }

    /**
     * 添加一个配置
     *
     * @param staticConfig
     */
    public static void addConfig(StaticConfig staticConfig) {
        configs.add(staticConfig);
    }

    /**
     * 移除一个配置
     *
     * @param staticConfig
     */
    public static void removeConfig(StaticConfig staticConfig) {
        configs.remove(staticConfig);
    }

    public static void clearCacheAll() {


    }

    public static void clearCache(String name, String... parameter) {

    }

    public static List<StaticConfig> getConfigs() {
        return configs;
    }

    public static void setConfigs(List<StaticConfig> configs) {
        StaticManger.configs = configs;
    }

    public static boolean isIsStatic() {
        return isStatic;
    }

    public static void setIsStatic(boolean isStatic) {
        StaticManger.isStatic = isStatic;
    }

    public static boolean hasExistencePage(StaticConfig staticConfig, HttpServletRequest request) {
        String basePath = request.getServletContext().getRealPath("/");//获取项目路径
        String relativePath = staticConfig.getFilePath();
        File file = new File(basePath + relativePath);
        if (file.exists()) {
            return true;
        }
        return false;
    }

    public static void forward(StaticConfig staticConfig, ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String basePath = request.getServletContext().getRealPath("/");//获取项目路径
        String relativePath = staticConfig.getFilePath();
        File file = new File(basePath + relativePath);
        if (file.exists()) {
            response.sendRedirect(relativePath);
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    /**
     * 页面重定向
     *
     * @param staticConfig
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws IOException
     * @throws ServletException
     */
    public static boolean forward(StaticConfig staticConfig, ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String basePath = request.getServletContext().getRealPath("/");//获取项目路径
        String relativePath = staticConfig.getFilePath();
        File file = new File(basePath + File.separatorChar + StaticManger.getPath() + request.getRequestURI() + File.separatorChar + relativePath);
        if (file.exists()) {
            if (StaticManger.isIsDebug()) {
                System.out.println("找到页面,执行跳转:" + relativePath);
            }
            request.getRequestDispatcher(StaticManger.getPath() + "/" + request.getRequestURI() + relativePath).forward(request, response);
            return false;
        }
        if (StaticManger.isIsDebug()) {
            System.out.println("没有找到页面,跳转控制器等待静态化");
        }
        request.setAttribute(StaticConstant.STATIC_MARKING, relativePath);
        return true;
    }

    public static boolean clear(StaticConfig staticConfig, ServletRequest servletRequest, ServletResponse servletResponse) {
        System.out.println("执行清理操作");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String basePath = request.getServletContext().getRealPath("/");//获取项目路径
        String relativePath = staticConfig.getFilePath();
        File file = new File(basePath + File.separatorChar + StaticManger.getPath() + staticConfig.getGenerateUrl().getUrl() + File.separatorChar + relativePath);
        if (file.exists()) {
            if (StaticManger.isIsDebug()) {
                System.out.println("找到页面,执行删除操作:" + relativePath);
            }
            file.delete();
            return true;
        }
        return true;
    }
    public static boolean isIsDebug() {
        return isDebug;
    }

    public static void setIsDebug(boolean isDebug) {
        StaticManger.isDebug = isDebug;
    }

    public static String getPath() {
        return path;
    }

    public static void setPath(String path) {
        StaticManger.path = path;
    }
}
